﻿Shader "Custom/begin/specular_1" {
	Properties {
		_Color("Color",Color) = (1.0,1.0,1.0,1.0)
		_SpecColor("Specular Color",Color) = (1.0,1.0,1.0,1.0)
		_Shininess("Shininess",Float) = 10.0
	}
	SubShader {
		Pass{
			Tags { "LightMode" = "ForwardBase" }
			
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			//user define
			uniform float4 _Color;
			uniform float4 _SpecColor;
			uniform float _Shininess;
			
			//unity define
			uniform float4 _LightColor0;
			//_WorldSpaceCameraPos
			
			struct vertexInput
			{
				float4 vertex: POSITION;
				float3 normal:NORMAL;
			};

			struct vertexOutput
			{
				float4 pos: SV_POSITION;
				float4 col : Color;
			};

			vertexOutput vert(vertexInput v)
			{
				vertexOutput o;
				//vertex
				float3 normalDirection = normalize(mul(float4(v.normal, 0.0) , _World2Object).xyz);
				//float3 viewDirection =  normalize( float3( float4( _WorldSpaceCameraPos, 1.0)  - mul( UNITY_MATRIX_MVP,v.vertex).xyz));
				float3 viewDirection =  normalize( _WorldSpaceCameraPos - mul( _Object2World, v.vertex).xyz);
				
				//light
				float3 lightDirection  = normalize( _WorldSpaceLightPos0.xyz); 
				float atten = 1.0;
				float3 diffuseReflection
				    = atten 
				    	* _LightColor0.xyz 
				    		* max(0.0, dot(normalDirection, lightDirection));
				float3 specularReflection 
					= atten 
						* _SpecColor.rgb 
						 	*  max(0.0, dot(normalDirection, lightDirection))
							 	* pow(
							 		 max( 0, dot(reflect( -lightDirection,normalDirection), viewDirection)),
							 		 	_Shininess) ;
				float3 lightFinal = diffuseReflection + specularReflection +  UNITY_LIGHTMODEL_AMBIENT;
				//o.col = float4(lightFinal * _Color.rgb ,1.0);
				o.col = float4(
							  float3(
							  	pow(
							 		 max( 0, dot(reflect( -lightDirection,normalDirection), viewDirection)),
							 		 	_Shininess) ) * _Color.rgb ,1.0);
				o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
				return o;
			}

			float4 frag(vertexOutput o) : Color
			{
				return o.col;
			}
			ENDCG
		}
	} 
	FallBack "Diffuse"
}
